﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TimKiemNhaTro.DTO;
using System.Data;
using KZ.Utilities;

namespace TimKiemNhaTro.DAL
{
    public class DiaDiemDAL
    {
        private static DiaDiemDTO LayDiaDiemTuDataReader(IDataReader reader)
        {
            DiaDiemDTO item = new DiaDiemDTO();

            if (!reader.IsDBNull(reader.GetOrdinal(DiaDiemColumns.MaDiaDiem.ToString()))) item.MaDiaDiem = Utility.IntParse(reader[DiaDiemColumns.MaDiaDiem.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(DiaDiemColumns.SoNha.ToString()))) item.SoNha = Utility.StringParse(reader[DiaDiemColumns.SoNha.ToString()]);
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(DiaDiemColumns.MaDuong.ToString()))) item.MaDuong = Utility.IntParse(reader[DiaDiemColumns.MaDuong.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(DiaDiemColumns.MaPhuong.ToString()))) item.MaPhuong = Utility.IntParse(reader[DiaDiemColumns.MaPhuong.ToString()]);
            }
            catch { };
            if (!reader.IsDBNull(reader.GetOrdinal(DiaDiemColumns.KinhDo.ToString()))) item.KinhDo = float.Parse(reader[DiaDiemColumns.KinhDo.ToString()].ToString());
            if (!reader.IsDBNull(reader.GetOrdinal(DiaDiemColumns.ViDo.ToString()))) item.ViDo = float.Parse(reader[DiaDiemColumns.ViDo.ToString()].ToString());
            return item;
        }
       /// <summary>
       /// Lay toan bo danh sach dia diem
       /// </summary>
       /// <returns></returns>
        public static List<DiaDiemDTO> DanhSachDiaDiem()
        {
            List<DiaDiemDTO> DanhSachDiaDiem= new List<DiaDiemDTO>();
            try
            {
                using (IDataReader reader = DataAccessHelper.ExecuteQuery("dbo.DANHSACHDIADIEM"))
                {
                    while (reader.Read())
                    {
                        DanhSachDiaDiem.Add(LayDiaDiemTuDataReader(reader));
                    }
                }
                return DanhSachDiaDiem;
            }
            catch (Exception e)
            {
                throw new Exception("Loi xay ra trong qua trinh lay danh sach dia diem" + e.Message);

            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
        /// <summary>
        /// Lay 1 dia diem theo ma dia diem
        /// </summary>
        /// <param name="MaDiaDiem"></param>
        /// <returns></returns>
        public static DiaDiemDTO LayDiaDiemTheoMaDiaDiem(string MaDiaDiem)
        {
            DiaDiemDTO diadiemDTO= new DiaDiemDTO();
            try
            {
                using (IDataReader reader = DataAccessHelper.ExecuteQuery("LayDiaDiemTheoMa"))
                {
                    while (reader.Read())
                    {
                        diadiemDTO = LayDiaDiemTuDataReader(reader);
                    }
                    return diadiemDTO;
                }
            }
            catch (Exception e)
            {
                throw new Exception("Co loi trong qua trinh lay thong tin dia diem" + e.Message);
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }

        }
        /// <summary>
        /// Tim Kiem dia diem theo nhieu tieu chi khac nhau (theo phuong, quan, ten dia diem)
        /// </summary>
        /// <param name="TenDiaDiem"></param>
        /// <param name="MaPhuong"></param>
        /// <param name="MaQuan"></param>
        /// <returns></returns>
        public static List<DiaDiemDTO> TimKiemDiaDiem(string TenDiaDiem, int MaPhuong, int MaQuan)
        {
            List<DiaDiemDTO> DanhSachTimKiem = new List<DiaDiemDTO>();
            try
            {
                DataAccessHelper.AddParameter("@MaQuan", SqlDbType.Int, MaQuan);
                DataAccessHelper.AddParameter("@MaPhuong", SqlDbType.Int, MaPhuong);
                DataAccessHelper.AddParameter("TenDiaDiem", SqlDbType.NVarChar, TenDiaDiem);
                using (IDataReader reader = DataAccessHelper.ExecuteQuery("dbo.TimKiemDiaDiem"))
                {
                    while (reader.Read())
                    {
                        DanhSachTimKiem.Add(LayDiaDiemTuDataReader(reader));
                    }
                }
                return DanhSachTimKiem;
            }
            catch (Exception e)
            {
                throw new Exception("Co loi trong qua trinh tim kiem" + e.Message);
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        
        
        }
    }
}